//
//  MM_HomeManageVC.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/20.
//

#import "MM_HomeManageVC.h"
#import "MM_HomeManageHeaderView.h"
#import "MM_Custom_MutiTabMenuView.h"
#import "MM_RefeshProtocol.h"
#import "MM_HomeStockListVC.h"
#import "MM_HomeStockWarnVC.h"
#import "MM_HomeNewListVC.h"
#import "MM_HomeLeftMaskView.h"
#import "MM_CodeScanVC.h"
#import "MM_HomeWelfareGoodsListVC.h"
#import "MM_HomeDataStatisticsVC.h"
#import "MM_AlertView.h"

@interface MM_HomeManageVC ()<MM_Custom_MutiTabMenuViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) MM_HomeManageHeaderView *headerNavView;
@property (nonatomic,strong) MM_Custom_MutiTabMenuView *menuView;
@property (nonatomic,strong) UIScrollView *contentScrollView;
@property (nonatomic,strong) MM_HomeLeftMaskView *leftMaskView;

@end

@implementation MM_HomeManageVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadVersionData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleView.hidden = YES;
    
    [self.view addSubview:self.headerNavView];
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.contentScrollView];
    [self.view addSubview:self.leftMaskView];
    
    self.leftMaskView.MM_x = -ScreenW;
    [self setupChildVCs];
}

- (void)loadVersionData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    [MM_Http getRequestURLStr:KMM_Sysversion withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        
        NSString *version_code = [requestDic[@"version_code"] description];
        NSString *is_force = [requestDic[@"is_force"] description];
        NSString *update_content = [requestDic[@"update_content"] description];
        WS(weakSelf);
        if (version_code.integerValue > LIOS_Version_Code) {
            
            if (is_force.integerValue == 1) {
                
                [MM_AlertView MM_AlertWithTitle:@"版本更新" message:update_content withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
              
                    if (buttonIndexType == MM_AlertViewBtnTypeEnsure) {
                        
                        [weakSelf jumpTestFlight];
                    }
                } cancelButtonTitle:nil ensureButtonTitle:@"确定"];
                
            } else {
                
                [MM_AlertView MM_AlertWithTitle:@"版本更新" message:update_content withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
              
                    if (buttonIndexType == MM_AlertViewBtnTypeEnsure) {
                        
                        [weakSelf jumpTestFlight];
                    }
                } cancelButtonTitle:@"取消" ensureButtonTitle:@"确定"];
            }
        }
        
        NSLog(@"requestDicrequestDicrequestDic %@",requestDic);
                
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self showStateText:errorInfo];
    }];
}

- (void)setupChildVCs {
  
  for (int i = 0; i < self.menuView.titleArr.count; i++) {

      if (i == 0) {
          MM_HomeStockListVC *listVC = [[MM_HomeStockListVC alloc] init];
          CGFloat view_w = self.contentScrollView.MM_w;
          CGFloat view_h = self.contentScrollView.MM_h;
          listVC.view.frame = CGRectMake(view_w * i , 0, view_w, view_h);
          [self addChildViewController:listVC];
          [self.contentScrollView addSubview:listVC.view];
          
      } else if (i == 1) {
          
          MM_HomeStockWarnVC *listVC = [[MM_HomeStockWarnVC alloc] init];
          CGFloat view_w = self.contentScrollView.MM_w;
          CGFloat view_h = self.contentScrollView.MM_h;
          listVC.view.frame = CGRectMake(view_w * i , 0, view_w, view_h);
          [self addChildViewController:listVC];
          [self.contentScrollView addSubview:listVC.view];
          
      }else if (i == 2) {
          MM_HomeNewListVC *listVC = [[MM_HomeNewListVC alloc] init];
          CGFloat view_w = self.contentScrollView.MM_w;
          CGFloat view_h = self.contentScrollView.MM_h;
          listVC.view.frame = CGRectMake(view_w * i , 0, view_w, view_h);
          [self addChildViewController:listVC];
          [self.contentScrollView addSubview:listVC.view];
          
      }else {
          MM_HomeWelfareGoodsListVC *listVC = [[MM_HomeWelfareGoodsListVC alloc] init];
          CGFloat view_w = self.contentScrollView.MM_w;
          CGFloat view_h = self.contentScrollView.MM_h;
          listVC.view.frame = CGRectMake(view_w * i , 0, view_w, view_h);
          [self addChildViewController:listVC];
          [self.contentScrollView addSubview:listVC.view];
          
      }
  }
}

#pragma mark -------- 代理 ------------
- (void)mutiTabView:(MM_Custom_MutiTabMenuView *)menuView sectionViewSelectedIndex:(NSInteger)index {
    [self.contentScrollView setContentOffset:CGPointMake(index*self.contentScrollView.MM_w, 0) animated:YES];
//    [self ref reshDataFromIndex:index];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = (NSInteger)(scrollView.contentOffset.x / scrollView.MM_w);
    [self.menuView setupSelectedIndex:index];
//    [self refreshDataFromIndex:index];
}

//通过index刷新控制器数据
//- (void)refreshDataFromIndex:(NSInteger)index {
//
//  if (index < self.childViewControllers.count) {
//
//    id<MM_RefeshProtocol>vc = self.childViewControllers[index];
//
//    [vc refreshData];
//  }
//}

- (MM_Custom_MutiTabMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[MM_Custom_MutiTabMenuView alloc] initWithFrame:CGRectMake(0, self.headerNavView.MM_Max_Y, ScreenW, 44)];
        _menuView.sameDivide = YES;
//        _menuView.noDivideSpace = 67;
        _menuView.lineViewSameH = 40;
        _menuView.titleArr = @[@"库存列表",@"库存预警",@"新品",@"福利品"];;
        _menuView.delegate = self;
    }
    return _menuView;
}

- (UIScrollView *)contentScrollView {
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.menuView.MM_Max_Y, ScreenW, ScreenH - HomeIndicatorHeight - NaviBarHeight - self.menuView.MM_h)];
        _contentScrollView.delegate = self;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.contentSize = CGSizeMake(self.menuView.titleArr.count *self.menuView.MM_w , 0);
        _contentScrollView.scrollEnabled = NO;
    }
    return _contentScrollView;
}

- (MM_HomeManageHeaderView *)headerNavView {
    
    if (_headerNavView == nil) {
        
        _headerNavView = [[MM_HomeManageHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NaviBarHeight)];
        WS(weakSelf);
        _headerNavView.headerBlock = ^(NSInteger index) {
            
            if (index == 0) {
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    weakSelf.leftMaskView.MM_x = 0;
                }];
                
            } else if (index == 1) {
                
                MM_CodeScanVC *codeVC = [[MM_CodeScanVC alloc] init];
                [weakSelf.navigationController pushViewController:codeVC animated:YES];
            }else {
                
            }
        };
    }
    return _headerNavView;
}

- (MM_HomeLeftMaskView *)leftMaskView {
    
    if (_leftMaskView == nil) {
        
        _leftMaskView = [[MM_HomeLeftMaskView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        WS(weakSelf);
        _leftMaskView.leftMaskBlock = ^(NSInteger index) {
            
            if (index == 0) {
                
                [weakSelf jumpTestFlight];
                
            } else if (index == 1) {
               
                [MM_AlertView MM_AlertWithTitle:@"" message:@"是否确定退出登录!" withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
                        
                    if (buttonIndexType == MM_AlertViewBtnTypeEnsure) {

                         [[MM_Cache shareMM_Cache] removeAllCustomInfo];
                        [MMNotificationCenter postNotificationName:MM_LogOutSuccessfully object:nil];
                    }
                } cancelButtonTitle:@"取消" ensureButtonTitle:@"确定"];
                
            }else if (index == 2) {
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    weakSelf.leftMaskView.MM_x = -ScreenW;
                    
                } completion:^(BOOL finished) {
                    
                    MM_HomeDataStatisticsVC *codeVC = [[MM_HomeDataStatisticsVC alloc] init];
                    [weakSelf.navigationController pushViewController:codeVC animated:YES];
                }];

            }else {
                [UIView animateWithDuration:0.5 animations:^{
                    
                    weakSelf.leftMaskView.MM_x = -ScreenW;
                }];
            }
        };
    }
    return _leftMaskView;
}

- (void)jumpTestFlight {
    
    NSString *urlString = @"beta.itunes.apple.com/v1/app/6465894068";
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-beta://"]]) {
        urlString = [NSString stringWithFormat:@"itms-beta://%@",urlString];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }else{
        urlString = [NSString stringWithFormat:@"https://%@",urlString];
        NSURL*url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }
}

@end
                                                                                                                                       
